package sjtu.ist.warehouse4ws.epc2bpmn;

import java.io.File;

import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import sjtu.ist.warehouse4ws.model.Epcunits;








public class GenerateBpmn {
	public String dynamicPath;


	public String getDynamicPath() {
		return dynamicPath;
	}

	public void setDynamicPath(String dynamicPath) {
		this.dynamicPath = dynamicPath;
	}
	public  String writeBpmn(String outFile,Epcunits list) throws Exception{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc=db.newDocument();
		
		Epcunits sourceDatas=new Epcunits();
		sourceDatas.setEpcUnits(list.getEpcUnits());
		
		
		WriteUtil writeUtil=new WriteUtil(doc,sourceDatas);
		
		writeUtil.write();
	
		return domDocToFile(doc,outFile,"UTF-8");	
	}
	
	
	
	public static String domDocToFile(Document doc, String fileName,String encoding) throws TransformerException{
		TransformerFactory tfactory=TransformerFactory.newInstance();
		Transformer transformer=tfactory.newTransformer();
		Properties properties=transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, encoding);
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.INDENT, "yes");            //control line
		transformer.setOutputProperties(properties);
		
		DOMSource source=new DOMSource(doc);
		File file=new File(fileName);
		StreamResult result=new StreamResult(file);
		
		transformer.transform(source, result);

		return file.getAbsolutePath();
	}
	

}
